<template>
  <div class="layout-all">
    <control :config="config" />
    <div class="layout-header" style="float:left">
      <el-button
        type="primary"
        icon="el-icon-plus"
        style="margin-left:0px"
        @click="onAdd"
      >添加</el-button>
    </div>
    <div class="layout-content">
      <el-table
        :data="tableData"
        stripe
        align="center"
        highlight-current-row
        style="width: 100%;margin-top:50px"
      ><el-table-column align="center" prop="id" label="序号" width="70" :fixed="isMobile()?'left':undefined"/>
        <el-table-column align="center" prop="title" label="商品名" />
        <el-table-column align="center" prop="create_time" label="创建时间" width="250" />
        <el-table-column align="center" prop="update_time" label="修改时间" width="250" />
        <el-table-column align="center" prop="sort" label="排序" width="120" />
        <el-table-column
          align="center"
          prop="telephone"
          :fixed="isMobile()?undefined:'right'"
          width="240"
          label="操作"
        >
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="mini"
              icon="el-icon-edit"
              @click="onEdit(scope.row.id)"
            />
            <el-button
              type="danger"
              size="mini"
              icon="el-icon-delete"
              @click="onDel(scope.row.id)"
            />
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-pagination
      :current-page="current_page"
      :page-size="per_page"
      layout="total, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <el-dialog
      title="提示"
      :visible.sync="visible"
      :width="isMobile()?'80%':'30%'"
    >
      <el-form ref="form" :rules="rules" :model="form" label-width="80px">
        <el-form-item label="商品名:" prop="title">
          <el-input v-model="form.title" placeholder="请输入商品名" />
        </el-form-item>
        <el-form-item label="排序:" prop="sort">
          <el-input v-model="form.sort" placeholder="请输入排序" type="number" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取 消</el-button>
        <el-button type="primary" @click="onSubmit('form')">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import control from '../../components/control/control'
export default {
  name:'hot_search_form',
  components: { control },
  data() {
    return {
      visible: false,
      config: [
        { type: 'text', placeholder: '请输入商品名', field: 'title', val: '' }
      ],
      value: '',
      searchWhere: {
        title: ''
      },
      tableData: [],
      total: 0,
      current_page: 1,
      per_page: 14,
      form: {},
      rules: {
        title: [
          { required: true, message: '请输入商品名', trigger: 'blur' }
        ]
      }
    }
  },
  activated(){
    // if(this.$getUpdate()){
      this.onSearch()
    // }
  },
  created() {
    this.onSearch()
  },
  methods: {
    // 查询
    handleSizeChange(val) {
      this.onSearch()
    },
    handleCurrentChange(val) {
      this.current_page = val
      this.onSearch()
    },
    onSearch() {
      const form = {}
      form.page = this.current_page
      form.map = this.searchWhere
      this.get('/admin/HotSearch/index', form, 'POST').then(e => {
        this.tableData = e.list.data
        this.per_page = e.list.per_page
        this.total = e.list.total
        this.current_page = e.list.current_page
      })
    },
    // 添加
    onAdd() {
      this.form = {
        id: 0,
        title: '',
        sort: 0
      }
      this.visible = true
    },
    // 编辑
    onEdit(id) {
      this.get('/admin/HotSearch/info?id=' + id, {}, 'POST').then(e => {
        this.form = e.info.tpl
      })
      this.visible = true
    },
    // 提交
    onSubmit(formName) {
      this.$refs[formName].validate(valid => {
        this.get('/admin/HotSearch/save', this.form, 'POST').then(e => {
          if (valid) {
            this.$msg_ok('success', '保存成功')
            this.visible = false
            this.onSearch()
          }
        })
      })
    },
    // 修改状态
    change_status(id, field, val) {
      this.update(id, val, field, 'Ad').then(e => {
        e ? this.onSearch() : ''
      })
    },
    // 删除
    onDel(id) {
      this.$msg_confirm().then(e => {
        if (e) {
          this.get('/admin/HotSearch/del?id=' + id, this.form, 'POST').then(e => {
            this.onSearch()
          })
        }
      })
    }
  }
}
</script>

<style>
.layout-all {
  margin-top: 20px;
  margin-left: 10px;
}
.layout-content {
  margin-top: 20px;
}
</style>>
